#!/bin/bash

# 输出带样式的文本
function print_style {
    echo -e "\e[1;32m$1\e[0m"
}

# 执行命令并检查返回值
function run_command {
    "$@"
    local status=$?
    if [ $status -ne 0 ]; then
        echo -e "\e[1;31mCommand failed with exit code $status\e[0m"
        exit $status
    fi
}

# 安装 ClamAV
print_style "Step 1: 安装 ClamAV"
run_command rpm -ivh --prefix=/usr/local/clamav clamav-1.0.4.linux.x86_64.rpm
sleep 1

# 设置环境变量
print_style "Step 2: 设置环境变量"
echo 'PATH=/usr/local/clamav/bin:/usr/local/clamav/sbin:$PATH' > /etc/profile.d/path.sh
run_command source /etc/profile.d/path.sh
run_command echo "/usr/local/clamav/lib64/" | sudo tee -a /etc/ld.so.conf.d/clamav.conf
run_command ldconfig
sleep 1

# 创建目录
print_style "Step 3: 创建目录"
run_command mkdir -p /usr/local/clamav/logs
run_command mkdir -p /usr/local/clamav/update
run_command mkdir -p /usr/local/clamav/socket
touch /usr/local/clamav/logs/clamd.log
touch /usr/local/clamav/logs/freshclam.log
sleep 1

# 复制配置文件
print_style "Step 4: 复制配置文件"
run_command cp /usr/local/clamav/etc/clamd.conf.sample /usr/local/clamav/etc/clamd.conf
run_command cp /usr/local/clamav/etc/freshclam.conf.sample /usr/local/clamav/etc/freshclam.conf
sleep 1

# 配置 ClamAV
print_style "Step 5: 配置 ClamAV"
run_command sed -i '/^Example/ s/^/#/' /usr/local/clamav/etc/clamd.conf
run_command cat <<EOF >> /usr/local/clamav/etc/clamd.conf
LogFile /usr/local/clamav/logs/clamd.log
PidFile /usr/local/clamav/update/clamd.pid
DatabaseDirectory /usr/local/clamav/update
LocalSocket /usr/local/clamav/socket/clamd.socket
EOF

run_command sed -i '/^Example/ s/^/#/' /usr/local/clamav/etc/freshclam.conf
run_command cat <<EOF >> /usr/local/clamav/etc/freshclam.conf
DatabaseDirectory /usr/local/clamav/update
UpdateLogFile /usr/local/clamav/logs/freshclam.log
PidFile /usr/local/clamav/update/freshclam.pid
EOF
sleep 1

# 复制配置文件到 /usr/local/etc/
print_style "Step 6: 复制配置文件到 /usr/local/etc/"
run_command cp /usr/local/clamav/etc/*.conf /usr/local/etc/
sleep 1

# 更新 ld.so.conf
print_style "Step 7: 更新 ld.so.conf"
run_command echo '/usr/local/clamav/lib64' >> /etc/ld.so.conf
run_command ldconfig
sleep 1

# 创建 ClamAV 用户和组
print_style "Step 8: 创建 ClamAV 用户和组"
run_command groupadd clamav
run_command useradd -g clamav clamav
sleep 1

# 设置权限
print_style "Step 9: 设置权限"
run_command chown clamav.clamav /usr/local/clamav/logs/clamd.log 
run_command chown clamav.clamav /usr/local/clamav/logs/freshclam.log 
run_command chown clamav.clamav /usr/local/clamav/logs
run_command chown clamav.clamav /usr/local/clamav/update/
run_command chown clamav.clamav /usr/local/clamav/socket/
sleep 1

# 下载病毒库
print_style "Step 10: 下载病毒库"
run_command /usr/local/clamav/bin/freshclam
sleep 1

# 创建软链接
print_style "Step 11: 创建软链接"
run_command ln -s /usr/local/clamav/bin/clamscan /usr/local/bin/clamscan
run_command ln -s /usr/local/clamav/bin/freshclam /usr/local/bin/freshclam
sleep 1

# 启动 ClamAV 守护进程
print_style "Step 12: 启动 ClamAV 守护进程"
run_command freshclam --daemon
sleep 1

# 设置开机自启动
print_style "Step 13: 设置开机自启动"
run_command echo "/usr/local/clamav/bin/freshclam --daemon" >> /etc/rc.d/rc.local
run_command /usr/local/clamav/sbin/clamd
sleep 1

# 配置定时任务
print_style "Step 14: 配置定时任务"
(crontab -l ; echo "1 2 * * * /usr/local/clamav/bin/freshclam" ; echo "1 3 * * * /usr/local/clamav/bin/clamscan -r / --remove -l /var/log/clamscan.log") | crontab -
sleep 1

#测试
print_style "Stop 15: 测试/root目录"
run_command clamscan /root/
sleep 1

# 完成
print_style "Installation completed successfully!"
sleep 1